/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETSI_Lim.cc
  \date			september 2011
  \author		TNick

  \brief		Contains the implementation for ETSI_Lim class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QFontMetrics>
#include	<QPainter>
#include	<QPen>

#include	<sheet/ETSheetView.h>
#include	<sheet/ETSheet.h>
#include	<sheet/subitems/ETSI_Root.h>
#include	<sheet/subitems/ETSI_SText.h>

#include	"ETSI_Lim.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */
DDHC_CONTENT(ETSI_Lim)

/* ------------------------------------------------------------------------- */
ETSI_Lim::ETSI_Lim	( void )
	: ETSubItem()
{

	ETSI_SText * only_it;

	only_it = new ETSI_SText();
	ctr_block.append(only_it);

	only_it = new ETSI_SText();
	btm_block_near.append(only_it);

	only_it = new ETSI_SText();
	btm_block_far.append(only_it);

	btm_pos = 0;

	DDHC_CONSTRUCT;
	DDHC_SUBITEM_VARS;
	DDHC_VARIABLE(btm_pos);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETSI_Lim::~ETSI_Lim	( void )
{
	DDHC_DESTRUCT
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Lim::recalcSign		(void)
{
	QFontMetrics f_m(font());
	setSignWidth(f_m.width(QString("lim")) * 1.1);
	setSignHeight(f_m.height());
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Lim::hitTest			(HtData * h_data)
{
	if (ctr_block.hitEmbAid(h_data, 0, 0)) return;
	if (btm_block_near.hitEmbAid(
				h_data,
				-btm_block_near.width(),
				btm_pos)) return;
	if (btm_block_far.hitEmbAid(
				h_data,
				btm_block_far.posX(),
				btm_pos)) return;

	h_data->it_hitted = this;
	h_data->true_hit = true;
	/* h_data->rct_bbx already set by the caller */
	h_data->int_data = 0;

}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void				ETSI_Lim::paint		(
		ETSheetView * vp, QPainter * painter, QPointF pt_ref )
{

	PaintData	pd(vp, painter, pt_ref);
	prePaint(&pd);


	/// draw the lim text
	QRectF r_lim(
				pt_ref.x() - signWidth(),
				pt_ref.y() - signHeight() / 2,
				signWidth(),
				signHeight()
				);

	painter->drawText(r_lim, QString("lim"));



	/* the sigma sign */
	QPen pn_t = painter->pen();
	QPen new_p;
	new_p.setColor(pn_t.color());
	new_p.setCosmetic(false);
	new_p.setCapStyle(Qt::RoundCap);
	new_p.setStyle(Qt::SolidLine);
	new_p.setJoinStyle(Qt::RoundJoin);
	new_p.setWidth(1);

	painter->setPen(new_p);


	qreal h_h = arrowHalfH();
	qreal x2 = pt_ref.x() - btm_block_near.width();
	qreal y_pos_b = pt_ref.y() + btm_pos;

	painter->drawLine(
				x2 - arrowWidth(),
				y_pos_b,
				x2,
				y_pos_b);
	painter->drawLine(
				x2,
				y_pos_b,
				x2 - 2*h_h,
				y_pos_b - h_h);
	painter->drawLine(
				x2,
				y_pos_b,
				x2 - 2*h_h,
				y_pos_b + 1.5*h_h);


	painter->setPen(pn_t);



	QPointF bs_p;

	bs_p.setX( pt_ref.x() - btm_block_near.width() );
	bs_p.setY( pt_ref.y() + btm_pos );
	btm_block_near.paint(vp, painter, bs_p);

	bs_p.setX( pt_ref.x() + btm_block_far.posX() );
	bs_p.setY( pt_ref.y() + btm_pos );
	btm_block_far.paint(vp, painter, bs_p);

	ctr_block.paint(vp, painter, pt_ref);


	postPaint(&pd);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Lim::recompute	(bool b_full)
{


	if (b_full == false)
	{ /* only recompute if this element is "dirty" */

		/* the SIZE_DIRTY flag means that we should recompute the sigma size
  and all items that rely on this font. */
		if (isSizeDirty())
		{
			btm_block_near.recompute(true);
			btm_block_far.recompute(true);
			ctr_block.recompute(true);
		}
		else if (isKidSizeDirty())
		{
			if (btm_block_far.requireRecompute())
			{
				btm_block_far.recompute(b_full);
			}
			if (ctr_block.requireRecompute())
			{
				ctr_block.recompute(b_full);
			}
			if (btm_block_near.requireRecompute())
			{
				btm_block_near.recompute(b_full);
			}
		}
		else
		{
			return;
		}
	}
	else
	{ /* forced recompute */
		btm_block_far.recompute(b_full);
		ctr_block.recompute(b_full);
		btm_block_near.recompute(b_full);
	}


	/* get the size of graphics based on the font that is used */
	recalcSign();

	/* the center part has same reference point as this sub-item so
   nothing to do there */


	/* there are two chunks at the bottom; the position of the reference line
results from largest negated y */
	btm_pos = ( - btm_block_near.boundingBox().y() > - btm_block_far.boundingBox().y()) ?
				- btm_block_near.boundingBox().y() :
				- btm_block_far.boundingBox().y();
	btm_pos += signHeight() / 4;

	/* X position of far (left) chunk at the bottom */
	btm_block_far.setPosX(
				- btm_block_near.width() -
				arrowWidth() -
				btm_block_far.width());

	/* bottom limit */
	qreal btm_l = ( btm_block_near.boundingBox().bottom() + btm_pos );
	qreal candidate = ( btm_block_far.boundingBox().bottom() + btm_pos );
	btm_l = btm_l < candidate ? candidate : btm_l;
	btm_l = btm_l < ctr_block.boundingBox().bottom() ?
				ctr_block.boundingBox().bottom() : btm_l;

	/* top limit */
	qreal top_l = ctr_block.boundingBox().y();
	candidate = -signHeight() / 2;
	top_l = top_l > candidate ? candidate : top_l;

	/* left limit */
	qreal left_l = btm_block_far.posX() < -signWidth() ?
				btm_block_far.posX() :
				-signWidth();

	bbox.setCoords(left_l, top_l, ctr_block.width(), btm_l);


	/* reset the flags that demand a recompute */
	resetSizeFlags();

}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
ETSI_Lim *		ETSI_Lim::loadXML		(
		QDomElement dom_e)
{

	QDomElement		dom_it;
	ETSI_Lim *		ret_it;

#ifdef	APP_DEBUG
	/* type is assumed to be sum */

	dom_it = dom_e.firstChildElement(XML_TAG_TYPE);
	if (!dom_it.isNull())
	{
		Q_ASSERT_X(dom_it.text() == XML_SIT_SUM, __FUNCTION__,
				   QString("loadXML got an element of type <"
						   "%1> while expected type is " XML_SIT_SUM)
				   .arg(dom_it.text()).toLatin1()
				   );
	}
#endif

	/* create this element */
	ret_it = new ETSI_Lim();


	/* the one and only common data to all kinds of sub-items */
	dom_it = dom_e.firstChildElement(XML_TAG_COMM);
	if (!dom_it.isNull())
	{
		ETSubItem::loadXMLCmn(ret_it, dom_it);

		if (!dom_it.nextSiblingElement(XML_TAG_COMM).isNull())
		{
			qDebug() << "The input presented more than one "
					 << "common tags, when only one per sub-item element "
					 << "is allowed";
		}
	}

	/* load particular data */

	for (;;)
	{
		ETSI_Root::loadEbdXmlRoot(XML_TAG_BTM_LEFT,
								  &ret_it->btm_block_far, dom_e);
		ETSI_Root::loadEbdXmlRoot(XML_TAG_BTM_RIGHT,
								  &ret_it->btm_block_near, dom_e);
		ETSI_Root::loadEbdXmlRoot(XML_TAG_CONTENT,
								  &ret_it->ctr_block, dom_e);
		return ret_it;
	}
	delete ret_it;
	return NULL;


}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				ETSI_Lim::saveXML		(
		QDomElement dom_e)
{
	QDomElement dom_it2;

	/* save the type */
	QDomElement dom_it = dom_e.ownerDocument().createElement(XML_TAG_TYPE);
	dom_e.appendChild(dom_it);
	QDomText dom_tx = dom_e.ownerDocument().createTextNode(XML_SIT_FRACT);
	dom_it.appendChild(dom_tx);


	/* save common properties */
	dom_it = dom_e.ownerDocument().createElement(XML_TAG_COMM);
	dom_e.appendChild(dom_it);
	saveXMLCmn(dom_it);


	/* now specifics */
	dom_it = dom_e.ownerDocument().createElement(XML_TAG_BTM_LEFT);
	dom_e.appendChild(dom_it);
	dom_it2 = dom_e.ownerDocument().createElement(XML_TAG_SUBITEM);
	dom_it.appendChild(dom_it2);
	btm_block_far.saveXML(dom_it2);

	dom_it = dom_e.ownerDocument().createElement(XML_TAG_BTM_RIGHT);
	dom_e.appendChild(dom_it);
	dom_it2 = dom_e.ownerDocument().createElement(XML_TAG_SUBITEM);
	dom_it.appendChild(dom_it2);
	btm_block_near.saveXML(dom_it2);

	dom_it = dom_e.ownerDocument().createElement(XML_TAG_CONTENT);
	dom_e.appendChild(dom_it);
	dom_it2 = dom_e.ownerDocument().createElement(XML_TAG_SUBITEM);
	dom_it.appendChild(dom_it2);
	ctr_block.saveXML(dom_it2);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		ETSI_Lim::kidNavReq_up		(ETSheetView * vp, ETSubItem * kid)
{
	//	if (kid == &top_block)
	//	{
	//		if (fatherSubI() != NULL)
	//		{
	//			fatherSubI()->kidNavReq_up(vp, this);
	//			return true;
	//		}
	//	}
	//	else if (kid == &btm_block)
	//	{
	//		vp->setCurrentSubI(NULL, &top_block);
	//		return true;
	//	}
	Q_UNUSED(vp);
	Q_UNUSED(kid);

	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		ETSI_Lim::kidNavReq_down		(ETSheetView * vp, ETSubItem * kid)
{
	//	if (kid == &top_block)
	//	{
	//		vp->setCurrentSubI(NULL, &btm_block);
	//		return true;
	//	}
	//	else if (kid == &btm_block)
	//	{
	//		if (fatherSubI() != NULL)
	//		{
	//			fatherSubI()->kidNavReq_down(vp, this);
	//			return true;
	//		}
	//	}
	Q_UNUSED(vp);
	Q_UNUSED(kid);

	return false;
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
